package com.ruoyi.questionBank.domain;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.Excel;
import lombok.Data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class ExamPaper {
    private Integer id;
    private String name;
    private String remark;
    private Integer theoryStatus;
    private String questionIds;
    private String questionData;

    private List<Integer> questionIdList;
    private List<Question> questionDataList;

    private Date createTime;
    private Date updateTime;

    public void unloadAnswer(){
        this.questionData = "";
        questionDataList.forEach(q -> q.unloadAnswer());
    }

    public void setQuestionIds(String questionIds) {
        this.questionIds = questionIds;
        this.questionIdList = JSON.parseArray(questionIds ,Integer.class);
    }

    public void setQuestionData(String questionData) {
        this.questionData = questionData;
        this.questionDataList = JSON.parseArray(questionData ,Question.class);
    }

    public List<Integer> getQuestionIdList() {
        if(questionIdList == null && questionIds != null){
            setQuestionIds(questionIds);
        }
        if(questionIdList == null){
            questionIdList = new ArrayList<>(0);
        }
        return questionIdList;
    }

    public void setQuestionIdList(List<Integer> questionIdList) {
        this.questionIdList = questionIdList;
        this.questionIds = JSON.toJSONString(questionIdList);
    }

    public List<Question> getQuestionDataList() {
        if(questionDataList == null && questionData != null){
            setQuestionData(questionData);
        }
        if(questionDataList == null){
            questionDataList = new ArrayList<>(0);
        }
        return questionDataList;
    }

    public void setQuestionDataList(List<Question> questionDataList) {
        this.questionDataList = questionDataList;
        this.questionData = JSON.toJSONString(questionDataList);
        //
        List<Integer> qidList = new ArrayList<>();
        if (questionDataList != null && questionDataList.size() > 0) {
            questionDataList.forEach(item -> {
                qidList.add(item.getId());
            });
        }
        setQuestionIdList(qidList);
    }

    public void setDefault(){
        if(name == null){
            name = "试卷-" + DateUtil.format(new Date() ,"yyyyMMddHHmmss");
        }
        if(remark == null){
            remark = name;
        }
        createTime = new Date();
        updateTime = createTime;
    }

    /**
     * 插入试题
     * @param questions
     */
    public void insertQuestion(List<Question> questions){
        List<Question> qs = getQuestionDataList();
        List<Integer> qids = getQuestionIdList();

        questions = questions.stream().filter(q -> !qids.contains(q.getId())).collect(Collectors.toList());
        List<Integer> tmp = questions.stream().map(q -> q.getId()).collect(Collectors.toList());

        if(!tmp.isEmpty()) {
            qs.addAll(questions);
            qids.addAll(tmp);
        }else {
            throw new IllegalArgumentException("all question are already in the exam paper");
        }
        setQuestionDataList(qs);
        setQuestionIdList(qids);
    }

    /**
     * 插入试题
     * @param question
     * @param sort
     */
    public void insertQuestion(Question question ,Integer sort){
        List<Question> qs = getQuestionDataList();
        List<Integer> qids = getQuestionIdList();
        if(qids.contains(question.getId())){
            throw new IllegalArgumentException("The question["+ question.getId() +"] is already in the exam paper["+ id +"]");
        }

        if(sort != null){
            qs.add(sort ,question);
            qids.add(sort ,question.getId());
        }else {
            qs.add(question);
            qids.add(question.getId());
        }
        setQuestionDataList(qs);
        setQuestionIdList(qids);
    }

    /**
     * 移除试题
     * @param qid
     */
    public void removeQuestion(int qid){
        List<Integer> qids = getQuestionIdList();
        Integer tmp = qids.remove(qid);
        if(tmp != null){
            List<Question> qs = getQuestionDataList();
            qs = qs.stream().filter(q -> q.getId() != qid).collect(Collectors.toList());
            setQuestionDataList(qs);
            setQuestionIdList(qids);
        }
    }

    /**
     * 移除试题
     * @param qidList
     */
    public void removeQuestion(List<Integer> qidList){
        List<Integer> qids = getQuestionIdList();
        if(qids.removeAll(qidList)){
            List<Question> qs = getQuestionDataList();
            qs = qs.stream().filter(q -> !qidList.contains(q.getId())).collect(Collectors.toList());
            setQuestionDataList(qs);
            setQuestionIdList(qids);
        }
    }

    /**
     * 批改试卷
     * @return
     */
    public int correct(List<String> answers){
        int as = answers.size();
        List<Question> qs = getQuestionDataList();
        int right = 0;
        for(int i = 0 ; i < qs.size() ; i++){
            if(i < as) {
                if (qs.get(i).answerResult(answers.get(i))) {
                    right++;
                }
            }
        }
        return right;
    }

    /**
     * 试卷题目数量
     * @return
     */
    public int questionSize(){
        return getQuestionDataList().size();
    }

    /**
     * 答题正确百分比
     * @param answers
     * @return
     */
    public int answerAccuracy(List<String> answers){
        int right = correct(answers);
        int total = questionSize();
        return answerAccuracy(right);
    }

    public int answerAccuracy(int right){
        return (int)(((double)right / questionSize()) * 100);
    }
}
